﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Entities;
using Utility;
using BUS;

public partial class Administrator_dathang : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            load_data();
        }
    }
    private void load_data()
    {
        Literal1.Text = "Danh sách đơn đặt hàng";
        pager.PageSize = 15;
        Criteria c = new Criteria();
        int total = 0;
        string querystring = Request.QueryString.ToString();

        try
        {
            if (querystring.Contains("kh") == true)
            {
                int id = int.Parse(Request.QueryString["kh"].ToString());
                    c.add(Condition.AND, Tbl_DonHang_Columns.NguoiDat, Condition.EQUAL,
                        id);
                    Tbl_KhachHang_Entities kh = Tbl_KhachHang_BUS.GetByID(id);
                    Literal1.Text = "Danh sách đơn đặt hàng của khách hàng '" + kh.TaiKhoan + "'";
            }
        }
        catch { }

        try
        {
            if (querystring.Contains("dg") == true)
            {
                if (Request.QueryString["dg"].ToString() == "false")
                {
                    c.add(Condition.AND, Tbl_DonHang_Columns.DaGiao, Condition.EQUAL,
                        "false");
                    Literal1.Text = "Danh sách đơn đặt hàng 'Chưa giao'";
                }
                else if (Request.QueryString["dg"].ToString() == "true")
                {
                    c.add(Condition.AND, Tbl_DonHang_Columns.DaGiao, Condition.EQUAL,
                        "true");
                    Literal1.Text = "Danh sách đơn đặt hàng 'Đã giao'";
                }
            }
        }
        catch { }

        try
        {
            if (querystring.Contains("iddh") == true)
            {
                int id = int.Parse(Request.QueryString["iddh"].ToString());
                if (id >=0)
                {
                    c.add(Condition.AND, Tbl_DonHang_Columns.MaDH, Condition.EQUAL,
                        id);
                    Literal1.Text = "Danh sách đơn đặt hàng có mã hóa đơn '" + id.ToString() + "'";
                }
            }
        }
        catch { }

       

        try
        {
            if (txt_tk.Text != "")
            {
                c.add(Condition.AND, Tbl_DonHang_Columns.MaDH, Condition.LIKE,
                    txt_tk.Text);
                Literal1.Text = "Danh sách đơn đặt hàng có mã '" + txt_tk.Text + "'";
            }
        }
        catch { }

        Tbl_DonHang_Collection dhCollection =
               Tbl_DonHang_BUS.Get(c.Criter, pager.CurrentIndex, pager.PageSize,
               Tbl_DonHang_Columns.STT.ToString(), OrderDirection.ASC.ToString(), out total);
        Repeater1.DataSource = dhCollection;
        Repeater1.DataBind();

        pager.ItemCount = total;
    }

    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem ||
                  e.Item.ItemType == ListItemType.Item)
        {
            Tbl_DonHang_Entities dh = (Tbl_DonHang_Entities)e.Item.DataItem;
            Tbl_CTDH_Entities ctdh = Tbl_CTDH_BUS.GetByIDDH(dh.STT);
            Tbl_SanPham_Entities sp = Tbl_SanPham_BUS.GetByID(ctdh.SanPham);

            HyperLink hyp_nguoimua = (HyperLink)e.Item.FindControl("hyp_nguoimua");
            HyperLink hyp_madh = (HyperLink)e.Item.FindControl("hyp_madh");
            Literal lit_stt = (Literal)e.Item.FindControl("lit_stt");
            CheckBox chkb_dagiao = (CheckBox)e.Item.FindControl("chkb_dagiao");

            lit_stt.Text = dh.STT.ToString();

            if (dh.DaGiao == true)
            {
                chkb_dagiao.Checked = true;
            }
            else
            {
                chkb_dagiao.Checked = false;
            }
            
            Tbl_KhachHang_Entities kh = Tbl_KhachHang_BUS.GetByID(dh.NguoiDat);
            hyp_nguoimua.NavigateUrl = "~/Administrator/khachhang_ct.aspx?id=" + dh.NguoiDat.ToString();
            hyp_nguoimua.Text = kh.TaiKhoan;

            hyp_madh.NavigateUrl = "~/Administrator/dathang_chitiet.aspx?id=" + dh.STT.ToString();
            hyp_madh.Text = dh.MaDH.ToString();
        }
    }
    protected void bt_timkiem_Click(object sender, EventArgs e)
    {
        load_data();
    }
}
